package cn.dakaqi.web.cron.thread;

import cn.dakaqi.api.qnzyz.activity.BHActivityService;
import cn.dakaqi.entities.ActivitySign;
import cn.dakaqi.utils.BaseDAO;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * 将结束后的活动所有打卡记录提交至北航系统
 */
public class ImportActivitySign2BHThread implements Runnable
{
    Logger log = Logger.getLogger(ImportActivityInfo2BHThread.class);
    BaseDAO baseDAO;
    BHActivityService bhActivityService;

    String sql = "";
    List<ActivitySign> signs = null;

    public ImportActivitySign2BHThread(BaseDAO baseDAO, BHActivityService bhActivityService, List<ActivitySign> signs)
    {
        this.baseDAO = baseDAO;
        this.bhActivityService = bhActivityService;
        this.signs = signs;
    }

    @Override
    public void run()
    {
        try
        {
            if(null == this.signs || this.signs.size() ==0)
                return;

            log.info("----------------将活动编号为【】的所有打卡记录提交至北航系统开始-------------------");

            for(ActivitySign sign:signs)
            {
                if(sign.getRecordId()>0L)
                    continue;
                if(sign.getTimes() == 0)
                    continue;

                String volunteerCode = sign.getVolunteer().getVolunteerCode();
                if(StringUtils.isBlank(volunteerCode) || volunteerCode.startsWith("V"))
                    continue;


                long signId = sign.getId();
                //导入打卡记录
                importSign(sign.getActivity().getActivityCode(),volunteerCode,String.valueOf(sign.getTimes()), signId);
                //导入活动的签到记录
                //importQianDao(activityCode,volunteerCode, DateUtil.DefaultTimeFormatter.format(sign.getCreateTime()), String.valueOf(sign.getLng()), String.valueOf(sign.getLat()));
                //导入活动的报名记录
                //importActivityApply(activityCode, volunteerCode, DateUtil.DefaultTimeFormatter.format(sign.getActivity().getCreateTime()));
            }
            log.info("----------------将活动编号为【】的所有打卡记录提交至北航系统结束-------------------");
        } catch (Exception e)
        {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    /**
     * 导入活动报名记录
     * @param activityCode
     * @param volunteerCode
     * @param createTime
     * @throws Exception
     */
    private void importActivityApply(String activityCode,String volunteerCode,String createTime)
    {
        try
        {
            int regId = bhActivityService.reg(activityCode, volunteerCode, "1", createTime);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 导入签到记录
     * @param activityCode
     * @param volunteerCode
     * @param createTime
     * @param lng
     * @param lat
     * @throws Exception
     */
    private void importQianDao(String activityCode,String volunteerCode,String createTime,String lng,String lat)
    {
        try
        {
            int signId = bhActivityService.sign(activityCode,volunteerCode,"1",createTime,lng,lat);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 导入打卡记录
     * @param activityCode
     * @param volunteerCode
     * @param time
     * @param signId
     * @throws Exception
     */
    private void importSign(String activityCode,String volunteerCode,String time,long signId)
    {
        try
        {
            //导入活动的打卡记录
            int  recordId = this.bhActivityService.record(activityCode,volunteerCode,time,"0");
            sql = "update U_ACTIVITY_SIGN set RECORD_ID = "  +recordId + " where id = " + signId;
            System.out.println(sql);
            this.baseDAO.updateNativeSql(sql);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
